package com.headingdata.exercise;

import java.util.Random;
import java.util.Scanner;

public class Test9 {

    public static void main(String[] args) {
        //模拟双色球  投注号码由6个红球，一个蓝球组成。红球1-33，蓝球1-16
        //    一等奖  6红1蓝     最高1000w
        //    二等奖  6红0蓝     最高500w
        //    三等奖  5红1蓝     3000
        //    四等奖  5红0蓝     200
        //           4红1蓝
        //    五等奖  4红0蓝     10
        //           3红1蓝
        //           2红1蓝
        //    六等奖  1红1蓝     5
        //           0红1蓝
//        int[] numbers = createUserBetNumber();
        int i = 0;
        while (true){
            int[] numbers = generateWinningNumbers();
            int[] winningNumbers = generateWinningNumbers();
            printArray(numbers);
            printArray(winningNumbers);
            Boolean isWin = winningJudgment(numbers,winningNumbers);
            i++;
            if (isWin){
                System.out.println("您买了" + i + "次彩票，终于中奖了！");
                break;
            }
        }

    }
    public static void printArray(int[] array){
        String printContent = "[";
        for (int i = 0; i < array.length; i++) {
            printContent += i == array.length -1 ? array[i] : array[i] + ",";
        }
        System.out.println(printContent + "]");
    }
    public static int[] createUserBetNumber(){
        int[] numbers = new int[7];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 6; i++) {
            System.out.println("投注红球号码范围1-33,请输入第" + (i+1) +"个红球号码：");
            while (true){
                int number = scanner.nextInt();
                if(number < 1 || number > 33){
                    System.out.println("对不起，您输入的红球号码不在1-33范围之间，请重新输入：");
                }else if(numberExist(numbers,number)){
                    System.out.println("对不起，您输入的红球号码已经存在，重复了，请重新输入：");
                }else{
                    numbers[i] = number;
                    break;
                }
            }
        }
        System.out.println("投注蓝球号码范围1-16,请输入蓝球号码：");
        while (true) {
            int number = scanner.nextInt();
            if(number < 1 || number >16 ){
                System.out.println("对不起，您输入的蓝球号码不在1-16范围之间，请重新输入：");
            }else{
                numbers[6] = number;
                break;
            }
        }
        return numbers;
    }

    private static boolean numberExist(int[] numbers, int number) {
        for (int i = 0; i < numbers.length; i++) {
            if(number == numbers[i]){
                return true;
            }
        }
        return false;
    }

    public static int[] generateWinningNumbers(){
        int[] numbers = new int[7];
        Random random = new Random();
        for (int i = 0; i < numbers.length; i++) {
            while (true) {
                int number = random.nextInt(33)+1;
                if(!numberExist(numbers,number)){
                    numbers[i] = number;
                    break;
                }
            }
        }
        int blueNumber = random.nextInt(16)+1;
        numbers[6] = blueNumber;
        return numbers;
    }

     public static Boolean winningJudgment(int[] numbers1,int[] numbers2){
         int redCount = 0;
         int blueCount = 0;
         Boolean isWin = true;
         for (int i = 0; i < numbers1.length-1; i++) {
             for (int j = 0; j < numbers2.length-1; j++) {
                 if(numbers1[i] == numbers2[j]){
                     redCount++;
                     break;
                 }
             }
         }
         if(numbers1[6] == numbers2[6]){
             blueCount ++;
         }
         if(redCount == 6 && blueCount ==1){
             System.out.println("恭喜您中了一等奖,奖金1000w元!");
         } else if (redCount == 6) {
             System.out.println("恭喜您中了二等奖,奖金500w元!");
         }else if(redCount == 5 && blueCount == 1){
             System.out.println("恭喜您中了三等奖,奖金3000元!");
         } else if (redCount == 5 ||redCount == 4 && blueCount == 1) {
             System.out.println("恭喜您中了四等奖,奖金200元!");
         } else if (redCount == 4 ||redCount >= 2 && blueCount == 1 ) {
             System.out.println("恭喜您中了五等奖,奖金10元!");
         } else if (blueCount == 1 ) {
             System.out.println("恭喜您中了六等奖,奖金5元!");
         }else{
             System.out.println("感谢您为福利事业做贡献，谢谢惠顾！");
             isWin = false;
         }

        return isWin;
     }
}
